package com.collectplus.express.passport;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.model.PassportBean;
import com.collectplus.express.view.VerticalViewPager;

import java.util.ArrayList;
import java.util.List;

import droid.frame.utils.android.Log;

/**
 * Created by joker on 2017/11/7.
 * passport主activity，里面加载viewpager，进行左右滑动
 */
@Deprecated
public class PassportMainActivity extends BaseActivity2 {

    private VerticalViewPager mViewPager = null;
    private static PassportMainActivity instance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        instance = this;
        // 第二个方法在此参数无效
        overridePendingTransition(R.anim.in_bottom2top, 0);
        super.onCreate(savedInstanceState);
        Window win = getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        //         win.getDecorView().setMinimumWidth((int) (win.getWindowManager().getDefaultDisplay().getWidth() * 0.6));// 设置dialog的宽度
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // lp.horizontalMargin = 300;
        lp.gravity = Gravity.CENTER;
        win.setAttributes(lp);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_passport_main);
        super.findViewById();
        mViewPager = (VerticalViewPager) findViewById(R.id.view_pager);
//        mViewPager.setOrientation(DirectionalViewPager.VERTICAL);
        //判断是否存在虚拟键盘
        //不存在虚拟键盘
        //        if (!ScreenUtils.hasNavBar(this)) {
        //            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mViewPager.getLayoutParams();
        //            params.bottomMargin = Utils.dpToPx(50, getResources());
        //            mViewPager.setLayoutParams(params);
        //        }else {
        //            int height = ScreenUtils.getNavigationBarHeight(this);
        //            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mViewPager.getLayoutParams();
        //            if (height>=126){//发现华为的是126，moto是144
        //                params.bottomMargin = Utils.dpToPx(10, getResources());
        //            }else {
        //                params.bottomMargin = Utils.dpToPx(20, getResources());
        //            }
        //            mViewPager.setLayoutParams(params);
        //        }
        setDataForViewPager();
    }

    public static PassportMainActivity getInstance() {
        return instance;
    }

    /**
     * 给viewpager设置数据
     */
    private void setDataForViewPager() {
        int position = 0;
        final List<PassportDetailFragment> fragments = new ArrayList<>();
        int size = 0;
        ArrayList<PassportBean> passportList = (ArrayList<PassportBean>) getIntent().getSerializableExtra("passportBeans");
        if (passportList == null || passportList.size() == 0) {
            String parcelId = getIntent().getStringExtra("parcelId");
            size = 1;
            PassportDetailFragment fragment = new PassportDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putString("parcelId", parcelId);
            bundle.putInt("position", 0);
            fragment.setArguments(bundle);
            fragments.add(fragment);
            mViewPager.setOffscreenPageLimit(size);
        } else {
            position = getIntent().getIntExtra("position", 0);
            size = passportList.size();
            for (int i = 0; i < size; i++) {
                PassportDetailFragment fragment = new PassportDetailFragment();
                Bundle bundle = new Bundle();
                bundle.putString("parcelId", passportList.get(i).getParcelId());
                bundle.putInt("positionTag", i);
                fragment.setArguments(bundle);
                fragments.add(fragment);
            }
            mViewPager.setOffscreenPageLimit(2);
        }
        mViewPager.setFragmentItems(getSupportFragmentManager(), fragments.toArray(new Fragment[fragments.size()]));
        //        mViewPager.setAdapter(new PassportDetailAdapter(getFragmentManager(), fragments));
        mViewPager.setCurrentItem(position);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        //从评价页面返回，评论成功后返回
        if (requestCode == 1001) {
            Log.d("11111", "评论成功后销毁");
            finish();
        }
    }

    @Override
    public void finish() {
        super.finish();
        instance = null;
        overridePendingTransition(0, R.anim.out_top2bottom);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
